using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Slingshot : MonoBehaviour
{
    public static Slingshot Instance{get; private set;}
    
    private LineRenderer leftLineRenderer;//左边的线
    private LineRenderer rightLineRenderer;//右边的线
    
    private Transform leftPoint;//左边的位置
    private Transform rightPoint;//右边的位置
    private Transform centerPoint;//发射的位置
    
    private bool isDrawing = false;//是否正在绘制
    private Transform birdTransform;//鸟的位置
    
    private void Awake()
    {
        Instance = this;
        
        //获取左边的线
        leftLineRenderer = transform.Find("LeftLineRenderer").GetComponent<LineRenderer>();
        //获取右边的线
        rightLineRenderer = transform.Find("RightLineRenderer").GetComponent<LineRenderer>();
        
        //获取左边的位置
        leftPoint = transform.Find("LeftPoint");
        //获取右边的位置
        rightPoint = transform.Find("RightPoint");
        //获取发射的位置
        centerPoint = transform.Find("CenterPoint");
    }
    
    // Start is called before the first frame update
    void Start()
    {
        
        HideLine();//隐藏线
    }

    // Update is called once per frame
    void Update()
    {
        if (isDrawing)
        {
            DrawLine();
        }
    }

    //开始绘制
    public void StartDraw(Transform birdTransform)
    {
        isDrawing = true;
        this.birdTransform = birdTransform;//设置鸟的位置
        ShowLine();//显示线
    }
    
    //结束绘制
    public void EndDraw()
    {
        isDrawing = false;
        HideLine();//隐藏线
    }
    
    //绘制线
    private void DrawLine()
    {
        //设置线的位置
        leftLineRenderer.SetPosition(0, birdTransform.position);
        leftLineRenderer.SetPosition(1, leftPoint.position);
        
        rightLineRenderer.SetPosition(0, birdTransform.position);
        rightLineRenderer.SetPosition(1, rightPoint.position);
    }
    
    //获取发射位置
    public Vector3 GetCenterPoint()
    {
        return centerPoint.position;
    }
    
    //隐藏线
    public void HideLine()
    {
        leftLineRenderer.enabled = false;
        rightLineRenderer.enabled = false;
    }
    
    //显示线
    public void ShowLine()
    {
        leftLineRenderer.enabled = true;
        rightLineRenderer.enabled = true;
    }
}
